import {
	payH5API,
	alipayH5API
} from '@/request/index/index.js';
import {
	WXpayAPI,
	WXpayDYPAPI
} from '@/request/restaurant/index.js';
import {
	WXpayH5API
} from '@/request/recharge/index.js';
import {
	createOrderNewAPI,
	agOrderPayAPI,
} from '@/request/restaurant/index.js';
// 获取配置文件
import config from '@/static/js/config.js';
import {
	mapGetters,
} from 'vuex';
// 公共mixins
export default {
	computed:{
		...mapGetters({
			// 获取品牌Id
			BrandInfor: 'shopping/BrandInfor',
		}),
	},
	// 方法
	methods: {
		// 获取当前品牌配置
		Brand() {
			// console.log( this['BrandInfor'],'11111111111111');
			return config[`Brand_${this['BrandInfor']['brand_id']}`]
		},
		// 支付
		payment(data) {
			uni.showLoading({
				title: '加载中'
			});
			/**
			 * @param {String} order_sn订单号
			 * @param {Number} type订单类型
			 * @param {String} redirect_url回调地址(查询支付状态完成之后的回调地址,并非请求支付接口时的回调地址)
			 * @payment方法的回调地址是查询完订单状态之后要进入的页面
			 */
			
			let {
				type = 1,
				Number = 0,
				order_sn = '',
				redirect_url = '',
			} = data;
			
			console.log(data, '新支付方法参数');
			
			// 微信跳转H5下单支付时需要先获取支付参数然后跳转微信小程序支付
			if (getApp()['globalData']['parameters']?.isWechat) {
				this._H5WXPAY(data).then(res => {
					uni.hideLoading();
					console.log(res['data'], '微信支付参数');
					const miniPayRequest = {
						timeStamp: encodeURIComponent(res['data']['result']['timestamp']),
						nonceStr: encodeURIComponent(res['data']['result']['nonceStr']),
						package: encodeURIComponent(res['data']['result']['package']),
						signType: encodeURIComponent(res['data']['result']['signType']),
						paySign: encodeURIComponent(res['data']['result']['paySign']),
						order_sn: res['data']['out_trade_no'],
						type,
						business: 'restaurant',
						// 微信结算付款页面需要return_url字段
						return_url: redirect_url
					};
					console.log(encodeURIComponent(JSON.stringify(miniPayRequest)), '编码');
					// return
					wx.miniProgram.navigateTo({
						url: `/pages/wxpay/wxpay?data=${encodeURIComponent(JSON.stringify(miniPayRequest))}&isencodeURL=true`
					});
				})

			} else {
				// 测试
				// location.href = `/pages/pay/index?order_sn=${order_sn}&type=${type}&redirect_url=${redirect_url}`;
				// return
				
				// 反之直接获取支付链接
				// 回调地址添加标识。用于点击返回上一页
				// redirect_url = `${redirect_url}`;
				WXpayH5API({
					order_sn,
					type,
					redirect_url: `${getApp()['globalData']['urlfileName']}/pages/pay/index?order_sn=${order_sn}&type=${type}&redirect_url=${redirect_url}`
				}).then(res => {
					console.log(
						`${getApp()['globalData']['urlfileName']}/pages/pay/index?order_sn=${order_sn}&type=${type}&redirect_url=${redirect_url}`
					)
					console.log(res['data']['redirect_url'], '支付方法地址');
					location.href = res['data']['redirect_url'];
				})
			}
		},


		// H5在小程序环境下支付时每个接口不同所以封装为统一方法
		_H5WXPAY(PayPara) {
			// 点餐微信支付接口
			if (PayPara['type'] == 3) {
				let { order_sn: out_order_num, money } = PayPara;
				return WXpayAPI({ out_order_num, money });
			};

			// 电影票微信支付接口
			if (PayPara['type'] == 4) {
				let { order_sn: orderNo } = PayPara;
				return WXpayDYPAPI({ orderNo });
			}
		},


		/**
		 * @支付回调统一管理方法
		 * @查询订单状态完成之后的回调地址redirect_url
		 */
		pay_statepage(parameters, redirect_url) {
			// console.log(order_sn,'pay_statepage方法参数');
			let url = '';
			url += redirect_url;
			console.log(url, '判断跳转链接');
			// 拼接参数将对象转换为URL参数
			console.log('拼接参数')
			if (typeof parameters !== 'undefined') {
				for (let key in parameters) {
					if (url.indexOf('?') == -1) {
						url += `?${key}=${parameters[key]}`;
						continue;
					};
					url += `&${key}=${parameters[key]}`
				}
			};
			console.log(url, '地址');
			if (redirect_url) {
				uni.redirectTo({
					url
				})
			}
		},
		
		// 订单立即支付
		againpayment(order_sn,money = 0) {
			console.log(this['BrandInfor']['brand_id'], '品牌ID');
			let redirect_url;
			// 肯德基
			if (this['BrandInfor']['brand_id'] == 1 || this['BrandInfor']['brand_id'] == 5) {
				redirect_url = `/pages/restaurant/order/order-status/order-status`;
			} else if (this['BrandInfor']['brand_id'] == 10 || this['BrandInfor']['brand_id'] == 13) {
				redirect_url = `/pages/restaurant/order/order-details/details`
			}
			// 小程序再次支付
			// 微信跳转H5后的逻辑为必须携带token
			const isWechat = getApp()['globalData']['parameters']?.isWechat;
			if(isWechat){
				this.payment({
					order_sn,
					type:3,
					redirect_url,
					money,
				});
			}else{
				// H5再次支付
				agOrderPayAPI({
					order_sn,
					type: 3,
					redirect_url: `${getApp()['globalData']['urlfileName']}/pages/pay/index?order_sn=${order_sn}&type=${3}&redirect_url=${redirect_url}`
				}).then(data => {
					location.href = data['data']['redirect_url'];
				})
			}
		},
	},

}
